package com.codesuc.aichat.chatHistory.service.impl;

import com.codesuc.aichat.chatHistory.entity.ChatHistory;
import com.codesuc.aichat.chatHistory.mapper.ChatHistoryMapper;
import com.codesuc.aichat.chatHistory.service.ChatHistoryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

/**
* @Author: ZhangGongMing
* @CreateTime: 2025/09/23 12:56
* @Description: ChatHistory业务实现类
* @Version: 1.0
*/
@Service
public class ChatHistoryServiceImpl extends ServiceImpl<ChatHistoryMapper, ChatHistory> implements ChatHistoryService {

    @Autowired
	private ChatHistoryMapper chatHistoryMapper;

    @Override
    public List<ChatHistory> getChatHistoryByUserIdAndRoleId(Long userId, Long roleId, Boolean isDesc) {
        LambdaQueryWrapper<ChatHistory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ChatHistory::getUserId, userId)
                   .eq(ChatHistory::getRoleId, roleId);
        if (isDesc) {
            queryWrapper.orderByDesc(ChatHistory::getCreateTime);
        } else {
            queryWrapper.orderByAsc(ChatHistory::getCreateTime);
        }
        return this.list(queryWrapper);
    }

    @Override
    public Page<ChatHistory> getChatHistoryByUserIdAndRoleIdPage(Long userId, Long roleId, Integer pageNo, Integer pageSize) {
        Page<ChatHistory> page = new Page<>(pageNo, pageSize);
        LambdaQueryWrapper<ChatHistory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ChatHistory::getUserId, userId)
                   .eq(ChatHistory::getRoleId, roleId)
                   .orderByAsc(ChatHistory::getCreateTime);
        return this.page(page, queryWrapper);
    }

}